{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "924e7dcf-fa8c-491c-99d5-a95b49e83094",
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6854adc2-d7b1-4524-960d-f4f33140277e",
   "metadata": {},
   "source": [
    "### 🔘 Question Answering using `HuggingFace` 🤗 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bec8941f-537e-4cbf-9750-bb283b21c5af",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transformers import pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d3e0c1d1-91a5-4e12-a309-262f1fb1377a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Provider text over which we will generate questions\n",
    "\n",
    "text = \"Leo Messi, is an Argentine professional footballer who \\\n",
    "        plays as a forward for Paris Saint-Germain and captains \\\n",
    "        the Argentina national team. Widely regarded as one of \\\n",
    "        the greatest players of all time, Messi has won a record \\\n",
    "        seven Ballon d'Or awards\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f2f1a75f-8863-4414-90ae-2f5eeac25527",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup the pipeline for question answering\n",
    "reader = pipeline(task = \"question-answering\", \n",
    "                  model = \"distilbert-base-cased-distilled-squad\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b2a69df0-154c-48fa-88aa-2ef34757583e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Question that we want to ask\n",
    "question = \"Who is the GOAT of football?\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5aa2c4d8-9ac3-4d0a-8bb9-907f70809b61",
   "metadata": {},
   "outputs": [],
   "source": [
    "outputs = reader(question=question, context=text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3f45f111-0d66-4944-9b9b-4d029e00feeb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>score</th>\n",
       "      <th>start</th>\n",
       "      <th>end</th>\n",
       "      <th>answer</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.919524</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>Leo Messi</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      score  start  end     answer\n",
       "0  0.919524      0    9  Leo Messi"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Put the results in a DataFrame\n",
    "\n",
    "import pandas as pd\n",
    "pd.DataFrame([outputs])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ce27d9c-65b7-4359-90ab-8db8a897fc6b",
   "metadata": {},
   "source": [
    "And we know who is the GOAT!! 😀"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8747eac2-842d-4b97-b83e-f10b2134db87",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_twitter",
   "language": "python",
   "name": "env_twitter"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
